import { ISqlInsert, ISqlUpdate } from '../../global'
import MysqlServer from "../utils/connect";

// 添加用户
export const userAddDao = async (params: Array<string>): Promise<ISqlInsert> => {
    return await MysqlServer.sqlAsync<ISqlInsert, Array<string>>("INSERT INTO t_user(`userUID`, `email`, `secretKey`) VALUES(?, ?, ?)", params)
}
// 用户是否存在
export const userCountDao = async (params: Array<string>): Promise<Array<{ count: number }>> => {
    return await MysqlServer.sqlAsync<Array<{ count: number }>, Array<string>>("SELECT COUNT(*) AS `count` FROM t_user WHERE `email` = ?", params)
}
// 重置密码
export const userResetDao = async (params: Array<string>): Promise<ISqlUpdate> => {
    return await MysqlServer.sqlAsync<ISqlUpdate, Array<string>>("UPDATE t_user SET `password` = ? WHERE `email` = ?", params)
}
export interface IUser {
    userUID: string
    email: string
    secretKey: string
    name: string
    role: string
    profile: string
}
// 密码登录
export const passwordLoginDao = async (params: Array<string>): Promise<Array<IUser>> => {
    return await MysqlServer.sqlAsync<Array<IUser>, Array<string>>("SELECT `userUID`, `email`, `secretKey`, `role`, `profile`, `name` FROM t_user WHERE `email` = ? AND `password` = ?", params)
}
// 验证码登录
export const codeLoginDao = async (params: Array<string>): Promise<Array<IUser>> => {
    return await MysqlServer.sqlAsync<Array<IUser>, Array<string>>("SELECT `userUID`, `email`, `secretKey`, `role`, `profile`, `name` FROM t_user WHERE `email` = ?", params)
}
// 获取用户信息
export const tokenInfoDao = async (params: Array<string>): Promise<Array<IUser>> => {
    return await MysqlServer.sqlAsync<Array<IUser>, Array<string>>("SELECT `userUID`, `email`, `secretKey`, `role`, `profile`, `name` FROM t_user WHERE `email` = ?", params)
}
// 修改用户名
export const changeNameDao = async (params: Array<string>): Promise<ISqlUpdate> => {
    return await MysqlServer.sqlAsync<ISqlUpdate, Array<string>>("UPDATE t_user SET `name` = ? WHERE `email` = ?", params)
}
// 修改用户头像
export const changeProfileDao = async (params: Array<string>): Promise<ISqlUpdate> => {
    return await MysqlServer.sqlAsync<ISqlUpdate, Array<string>>("UPDATE t_user SET `profile` = ? WHERE `email` = ?", params)
}
// 查看用户状态
export const userStateDao = async (params: Array<string>): Promise<Array<{ state: number }>> => {
    return await MysqlServer.sqlAsync<Array<{ state: number }>, Array<string>>("SELECT `state` FROM t_user WHERE `email` = ?", params)
}